import * as React from "react"
import { type SearchParams } from "@/types/table"

import { getValidFilters } from "@/lib/data-table"
import { Skeleton } from "@/components/ui/skeleton"
import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton"
import { Shell } from "@/components/shell"

import { getVendorCandidateCounts, getVendorCandidates } from "@/lib/vendor-candidates/service"
import { searchParamsCandidateCache } from "@/lib/vendor-candidates/validations"
import { VendorCandidateTable } from "@/lib/vendor-candidates/table/candidates-table"
import { DateRangePicker } from "@/components/date-range-picker"
import { InformationButton } from "@/components/information/information-button"
import { useTranslation } from "@/i18n"
interface IndexPageProps {
  params: Promise<{lng: string}>
  searchParams: Promise<SearchParams>
}

export default async function IndexPage(props: IndexPageProps) {
  const searchParams = await props.searchParams
  const search = searchParamsCandidateCache.parse(searchParams)
  const {lng} = await props.params
  const {t} = await useTranslation(lng, 'menu')

  const validFilters = getValidFilters(search.filters)

  const promises = Promise.all([
    getVendorCandidates({
      ...search,
      filters: validFilters,
    }),
    getVendorCandidateCounts()
  ])

  return (
    <Shell className="gap-2">
      
      <div className="flex items-center justify-between space-y-2">
        <div className="flex items-center justify-between space-y-2">
          <div>
            <div className="flex items-center gap-2">
              <h2 className="text-2xl font-bold tracking-tight">
                {t('menu.vendor_management.candidates')}                
              </h2>
              <InformationButton pagePath="evcp/vendor-candidates" />
            </div>
            {/* <p className="text-muted-foreground">
              수집한 협력업체 후보를 등록하고 초대 메일을 송부할 수 있습니다.           
            </p> */}
          </div>
        </div>
      </div>
      
      {/* 수집일 라벨과 DateRangePicker를 함께 배치 */}
      <div className="flex items-center justify-start gap-2">
        {/* <span className="text-sm font-medium">수집일 기간 설정: </span> */}
        <React.Suspense fallback={<Skeleton className="h-7 w-52" />}>
          <DateRangePicker
            triggerSize="sm"
            triggerClassName="w-56 sm:w-60"
            align="end"
            shallow={false}
            showClearButton={true} 
            placeholder="수집일 날짜 범위를 고르세요"
          />
        </React.Suspense>
      </div>

      <React.Suspense
        fallback={
          <DataTableSkeleton
            columnCount={6}
            searchableColumnCount={1}
            filterableColumnCount={2}
            cellWidths={["10rem", "40rem", "12rem", "12rem", "8rem", "8rem"]}
            shrinkZero
          />
        }
      >
        <VendorCandidateTable promises={promises}/>
      </React.Suspense>
    </Shell>
  )
}